package com.ecloud.gardensterminal.controller;

import com.ecloud.gardensterminal.common.JsonResult;
import com.ecloud.gardensterminal.controller.dto.GardensThresholdDto;
import com.ecloud.gardensterminal.controller.dto.GardensThresholdQueryDto;
import com.ecloud.gardensterminal.domain.GardensThreshold;
import com.ecloud.gardensterminal.server.GardensThresholdService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Squbi
 */
@RestController
public class GardensThresholdController {
    @Autowired
    private GardensThresholdService thresholdService;

    @GetMapping("/threshold")
    public JsonResult getThreshold() {
        List<GardensThreshold> thresholds = thresholdService.getThresholds();
        return JsonResult.success(thresholds);
    }

    @GetMapping("/threshold/{deviceId}")
    public JsonResult getThreshold(@PathVariable("deviceId") Integer deviceId){
        GardensThreshold threshold = thresholdService.getThreshold(deviceId);
        return JsonResult.success(threshold);
    }

    @PostMapping("/threshold/query")
    public JsonResult queryThreshold(@RequestBody GardensThresholdQueryDto dto) {
        List<GardensThreshold> gardensThresholds = thresholdService.queryThreshold(dto);
        return JsonResult.success(gardensThresholds);
    }

    @ApiOperation("thresholdFiled对于不同类型的设备的字段不同\n" +
            "类型 1 天气 可选字段有: 湿度 weatherHumidity; 温度 weatherTemperature; 噪音 weatherNoise; pm2.5 weatherPm25; pm10 weatherPm10;\n" +
            "类型 2 土壤 可选字段有: 湿度 soilMoisture; 温度 soilTemperature; 导电 soilElectrical; 氮 soilNitrogen; 磷 soilPhosphorus; 钾 soilPotassium; 盐度 soilSalinity;\n")
    @PostMapping("/threshold")
    public JsonResult insertThreshold(@RequestBody GardensThresholdDto dto) {
        thresholdService.insertThreshold(dto);
        return JsonResult.success();
    }

    @PutMapping("/threshold")
    public JsonResult update(@RequestBody GardensThresholdDto dto) {
        thresholdService.updateThreshold(dto);
        return JsonResult.success();
    }

    @DeleteMapping("/threshold")
    public JsonResult deleteThreshold(Integer deviceId) {
        thresholdService.deleteThreshold(deviceId);
        return JsonResult.success();
    }
}
